package utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;

import bean.LoginBean;

/**
 * Created by Administrator on 2016/7/27.
 */
public class CarServerLoginDB {
    /**
     * 数据库名字
     */
    public static final String DB_NAME = "Login";
    /**
     * 数据库的版本
     */
    public static final int DB_VERSION = 1;

    private static CarServerLoginDB loginDB;

    private SQLiteDatabase db;

    /**
     * 将构造方法私有化
     *
     * @param context
     */
    public CarServerLoginDB(Context context) {
        CarServiceOpenHelper dbhelper = new CarServiceOpenHelper(context, DB_NAME, null, DB_VERSION);
        db = dbhelper.getReadableDatabase();
    }

    /**
     * 获取CarServerLoginDB的实例
     *
     * @param context
     * @return
     */
  /*  public synchronized static CarServerLoginDB getInstance(Context context) {
        if (loginDB == null) {
            loginDB = new CarServerLoginDB(context);
        }
        return loginDB;
    }*/

    /**
     * 将登录用户保存在数据库
     *
     * @param user
     */
    public void saveUser(LoginBean user) {
        Log.e("+++","插入0");
        ArrayList<String> arrayList= queryAll(user.getId());
        if(arrayList.size()==0){
            Log.e("+++","插入1");
            ContentValues values = new ContentValues();
            values.put("Login_id",user.getId());
            values.put("Login_phone", user.getPhone());
            values.put("Login_password", user.getPassword());
            values.put("Login_username", user.getUsername());
            values.put("Login_headPhoto", user.getHeadphoto());
            db.insert(DB_NAME, null, values);
        } else{
            if(!arrayList.contains(user.getId())){
                Log.e("+++","插入2");
                ContentValues values = new ContentValues();
                values.put("Login_id",user.getId());
                values.put("Login_phone", user.getPhone());
                values.put("Login_password", user.getPassword());
                values.put("Login_username", user.getUsername());
                values.put("Login_headPhoto", user.getHeadphoto());
                db.insert(DB_NAME, null, values);
            }
        }
        Log.e("++++",arrayList.toString());
    }

    /**
     * 修改用户资料
     * @param user
     */
    public int updataHeadPhoto(LoginBean user){
        ContentValues values = new ContentValues();
        values.put("Login_headPhoto", user.getHeadphoto());
        String whereClause = "Login_headPhoto" + " = ?";
        String[] whereArgs = {user.getHeadphoto()};
        return db.update(DB_NAME, values, whereClause, whereArgs);
    }

    public int updataUserName(LoginBean user){
        ContentValues values = new ContentValues();
        values.put("Login_username", user.getUsername());
        String whereClause = "Login_username" + " = ?";
        String[] whereArgs = {user.getUsername()};
        return db.update(DB_NAME, values, whereClause, whereArgs);
    }

    public ArrayList<String> queryAll(String id){
        String sql = "select * from Login where Login_id="+id;
        ArrayList<String> arrayList = new ArrayList<>();
        Cursor cursor = db.rawQuery(sql,null);
        while (cursor.moveToNext()){
            String Login_id = cursor.getString(cursor.getColumnIndex("Login_id"));
            String Login_phone = cursor.getString(cursor.getColumnIndex("Login_phone"));
            String Login_password = cursor.getString(cursor.getColumnIndex("Login_password"));
            String Login_username = cursor.getString(cursor.getColumnIndex("Login_username"));
            String Login_headPhoto = cursor.getString(cursor.getColumnIndex("Login_headPhoto"));
            //loginBean = new LoginBean(Login_id,Login_phone,Login_password,Login_username,Login_headPhoto);
            arrayList.add(Login_id);
        }
        Log.e("---",arrayList.toString());
        return arrayList;
    }

    public LoginBean queryId(){
        String sql = "select * from Login ";
        LoginBean loginBean=null;
        Cursor cursor = db.rawQuery(sql,null);
        while (cursor.moveToNext()){
            String Login_id = cursor.getString(cursor.getColumnIndex("Login_id"));
            String Login_phone = cursor.getString(cursor.getColumnIndex("Login_phone"));
            String Login_password = cursor.getString(cursor.getColumnIndex("Login_password"));
            String Login_username = cursor.getString(cursor.getColumnIndex("Login_username"));
            String Login_headPhoto = cursor.getString(cursor.getColumnIndex("Login_headPhoto"));
            loginBean = new LoginBean(Login_id,Login_phone,Login_password,Login_username,Login_headPhoto);
        }
        return loginBean;
    }
}
